home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / treewnd.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  11KB  |  415 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. #ifndef __TREEWND_H
  30. #define __TREEWND_H
  31.  
  32. // BU: lots of changes
  33. // - all items must be deletable, and will be deleted on destructor
  34. // - root items list not allocated w/ new
  35. // - items set sorting within their PtrListSorted instead of manually calling it
  36. // - setting an item to auto-sort does *not* make subitems autosort too
  37.  
  38. #include "scbkgwnd.h"
  39. #include "ptrlist.h"
  40. #include "editwnd.h"
  41. #include "common.h"
  42. #include "skinclr.h"
  43.  
  44. #define TREEWND_PARENT ScrlBkgWnd
  45.  
  46. #define STATUS_EXPANDED  0
  47. #define STATUS_COLLAPSED 1
  48.  
  49. #define HITTEST_BEFORE   0
  50. #define HITTEST_IN       1
  51. #define HITTEST_AFTER    2
  52.  
  53. #define LINK_RIGHT       1
  54. #define LINK_TOP         2
  55. #define LINK_BOTTOM      4
  56.  
  57. #define TAB_NO           FALSE
  58. #define TAB_YES          TRUE
  59. #define TAB_AUTO         2
  60.  
  61. #define WM_SETITEMDEFERRED WM_USER+6546
  62.  
  63. #define DC_SETITEM    10
  64. #define DC_DELITEM    20
  65. #define DC_EXPAND    30
  66. #define DC_COLLAPSE    40
  67.  
  68. // Forward references
  69.  
  70. class TreeItemList;
  71. class TreeItem;
  72. class TreeWnd;
  73.  
  74. class FontSize;
  75.  
  76. // classes & structs
  77.  
  78. class COMEXP TreeItem {
  79. friend TreeWnd;
  80. public:
  81.   TreeItem();
  82.   virtual ~TreeItem();
  83.  
  84.   virtual SkinBitmap *getIcon();
  85.   virtual void setIcon(SkinBitmap *newicon);
  86.  
  87.   // FUCKO: see if these are all implemented
  88.   virtual void onTreeAdd() {}
  89.   virtual void onTreeRemove() {}
  90.   virtual void onSelect() {}
  91.   virtual void onDeselect() {}
  92.   virtual int onLeftDoubleClick() { return 0; }
  93.   virtual int onRightDoubleClick() { return 0; }
  94.   virtual int onContextMenu(int x, int y);
  95.   virtual int onChar(UINT key) { return 0; }    // return 1 if you eat the key
  96.  
  97.   virtual int onBeginLabelEdit();
  98.   virtual int onEndLabelEdit(const char *newlabel);
  99.  
  100.   virtual void setLabel(const char *label);
  101.   virtual const char *getLabel();
  102.  
  103.   // override to draw by yourself. Return the width of what you've drawn
  104.   virtual int customDraw(Canvas *canvas, const POINT &pt, int defaultTxtHeight, int indentation, const RECT &clientRect); 
  105.  
  106.   // return 0 to refuse being dragged
  107.   // else return 1 and install the droptype and dropitem
  108.   virtual int onBeginDrag(char *suggestedTitle) { return 0; }
  109.  
  110.   virtual int dragOver(RootWnd *sourceWnd) { return 0; }
  111.   virtual int dragLeave(RootWnd *sourceWnd) { return 0; }
  112.   virtual int dragDrop(RootWnd *sourceWnd) { return 0; }
  113.  
  114.   virtual int dragComplete(int success) { return 0; }
  115.  
  116.   void ensureVisible();
  117.  
  118.   TreeItem *getNthChild(int nth); // enumerates children (zero based)
  119.   TreeItem *getChild();
  120.   TreeItem *getChildSibling(TreeItem *item);
  121.   TreeItem *getSibling();
  122.   TreeItem *getParent();
  123.  
  124.   void editLabel();
  125.     
  126.   int getNumChildren();
  127.   BOOL hasSubItems();
  128.  
  129.   void setSorted(int issorted);
  130.   void setChildTab(int haschildtab);
  131.   BOOL isSorted();
  132.  
  133.   BOOL isCollapsed();
  134.   BOOL isExpanded();
  135.  
  136.   void invalidate();
  137.   BOOL isSelected();
  138.   BOOL isHilited();
  139.   void setHilited(BOOL ishilited);
  140.  
  141.   int collapse();
  142.   int expand();
  143.  
  144.   int getCurRect(RECT *r);
  145.  
  146.   void setCurrent(BOOL tf);
  147.  
  148.   TreeWnd *getTree() const;
  149.  
  150. protected:
  151.  
  152.   BOOL isHilitedDrop();
  153.   void setHilitedDrop(BOOL ishilitedDrop);
  154.  
  155.   void linkTo(TreeItem *linkto);
  156. //  void childDeleted(TreeItem *child);
  157.   void setTree(TreeWnd *newtree);
  158.   void addSubItem(TreeItem *item);
  159.   void setCurRect(int x1, int y1, int x2, int y2, int z);
  160.   int getIndent();
  161.  
  162.   BOOL needTab();
  163.   void sortItems(); // sorts the children of this item
  164.   void setEdition(BOOL isedited);
  165.   BOOL getEdition();
  166.  
  167. private:
  168.   void setSelected(BOOL isselected, BOOL expandCollapse=FALSE, BOOL editifselected=FALSE);
  169.   // this really calls delete on the subitems
  170.   void deleteSubitems();
  171.  
  172.   int removeSubitem(TreeItem *item);
  173.  
  174.   int getItemWidth(int txtHeight, int indentation);
  175.  
  176.   String label;
  177.   class TreeItem *parent;
  178.   TreeItemList *subitems;    // children
  179.   RECT curRect;
  180.   int childTab;
  181.   TreeWnd *tree;
  182.   int expandStatus;
  183.   SkinBitmap *icon;
  184.   int _z;
  185.  
  186.   BOOL selected:1;
  187.   BOOL hilitedDrop:1;
  188.   BOOL hilited:1;
  189.   BOOL being_edited:1;
  190. };
  191.  
  192. class CompareTreeItem {
  193. public:
  194.   static int compareItem(TreeItem *p1, TreeItem *p2);
  195. };
  196.  
  197. class TreeItemList : public PtrListQuickSorted<TreeItem, CompareTreeItem> { };
  198.  
  199. class COMEXP TreeWnd : public TREEWND_PARENT {
  200.  
  201. friend TreeItem;
  202.  
  203. public:
  204.     
  205.   TreeWnd();
  206.   virtual ~TreeWnd();
  207.  
  208.   virtual int onInit();
  209.   virtual int onPaint(Canvas *canvas);
  210.   virtual int childNotify(RootWnd *child, int msg, int param1=0, int param2=0);
  211.  
  212.   virtual int onLeftButtonDown(int x, int y);
  213.   virtual int onLeftButtonUp(int x, int y);
  214.   virtual int onRightButtonUp(int x, int y);
  215.   virtual int onMouseMove(int x, int y);
  216.  
  217.   virtual int onLeftButtonDblClk(int x, int y);
  218.   virtual int onRightButtonDblClk(int x, int y);
  219.  
  220.   virtual int onMouseWheelUp(int clicked, int lines);
  221.   virtual int onMouseWheelDown(int clicked, int lines);
  222.   
  223.   virtual void timerCallback(int c);
  224.  
  225.   virtual int onContextMenu(int x, int y);
  226.   virtual int onDeferredCallback(int param1, int param2);
  227.  
  228.   virtual int onChar(char c);
  229.   virtual int onKeyDown(int keycode);
  230.   virtual void jumpToNext(char c);
  231.   void ensureItemVisible(TreeItem *item);
  232.  
  233.   // don't need to override this: just calls thru to the treeitem
  234.   virtual int onBeginDrag(TreeItem *treeitem);
  235.  
  236.   virtual int dragEnter(RootWnd *sourceWnd);
  237.   virtual int dragOver(int x, int y, RootWnd *sourceWnd);
  238.   virtual int dragLeave(RootWnd *sourceWnd);
  239.   virtual int dragDrop(RootWnd *sourceWnd, int x, int y);
  240.  
  241.   virtual int dragComplete(int success);
  242.  
  243.   int wantFocus() { return 1; }
  244.   virtual void freeResources();
  245.   virtual void reloadResources();
  246.  
  247.   virtual int compareItem(TreeItem *p1, TreeItem *p2);
  248.  
  249. protected:
  250.   // these will be called if the pointer is not over a treeitem
  251.   virtual int defaultDragOver(int x, int y, RootWnd *sourceWnd) { return 0; }
  252.   virtual int defaultDragDrop(RootWnd *sourceWnd, int x, int y) { return 0; }
  253.  
  254.   // called with item that received a drop
  255.   virtual void onItemRecvDrop(TreeItem *item) {}
  256.  
  257.   virtual void onLabelChange(TreeItem *item) {}
  258.  
  259.   virtual void onItemSelected(TreeItem *item) {}
  260.   virtual void onItemDeselected(TreeItem *item) {}
  261.  
  262.   virtual int onKillFocus();
  263.  
  264. public:
  265.  
  266.   virtual int getContentsWidth();
  267.   virtual int getContentsHeight();
  268.  
  269.   void setRedraw(BOOL r);
  270.  
  271.   TreeItem *addTreeItem(TreeItem *item, TreeItem *par, int sorted=TRUE, int haschildtab=FALSE);
  272.  
  273.   // just removes a TreeItem from the tree, doesn't delete it... this is for
  274.   // ~TreeItem to call only
  275.   int removeTreeItem(TreeItem *item);
  276.  
  277.   void moveTreeItem(TreeItem *item, TreeItem *newparent);
  278.  
  279.   void deleteAllItems();
  280.  
  281.   int expandItem(TreeItem *item);
  282.   void expandItemDeferred(TreeItem *item);
  283.   int collapseItem(TreeItem *item);
  284.   void collapseItemDeferred(TreeItem *item);
  285.  
  286.   void selectItem(TreeItem *item);    // selects.
  287.   void selectItemDeferred(TreeItem *item);// selects. posted.
  288.   void delItemDeferred(TreeItem *item);
  289.   void hiliteItem(TreeItem *item);
  290.   void unhiliteItem(TreeItem *item);
  291.   void setHilitedColor(const char *colorname);
  292.   COLORREF getHilitedColor();
  293.  
  294.   TreeItem *getCurItem();
  295.  
  296.   TreeItem *hitTest(POINT pos);
  297.   TreeItem *hitTest(int x, int y);
  298.  
  299.   virtual void setTip(const char *tip);
  300.   virtual const char *getTip();
  301.  
  302.   void editItemLabel(TreeItem *item);
  303.   void cancelEditLabel(int destroyit=0);
  304.   void setAutoEdit(int ae);
  305.   int getAutoEdit();
  306.   // use a NULL item to search all items. returns first item found
  307.   TreeItem *getByLabel(TreeItem *item, char *name);
  308.  
  309.   int getItemRect(TreeItem *item, RECT *r);
  310.  
  311.   int ownerDraw();
  312.  
  313.   int getNumRootItems();
  314.   TreeItem *enumRootItem(int which);
  315.  
  316.   void setSorted(BOOL dosort);
  317.   BOOL getSorted();
  318.  
  319.   void sortTreeItems();
  320.  
  321.   TreeItem *getSibling(TreeItem *item);
  322.  
  323.   TreeItem *getItemFromPoint(POINT *pt);
  324.  
  325.   void setAutoCollapse(BOOL doautocollapse);
  326.  
  327.   virtual int setFontSize(int newsize);
  328.   int getFontSize();
  329.  
  330.   int getNumVisibleChildItems(TreeItem *c);
  331.   int getNumVisibleItems();
  332.   TreeItem *enumVisibleItems(int n);
  333.   TreeItem *enumVisibleChildItems(TreeItem *c, int n);
  334.  
  335. protected:
  336.   void hiliteDropItem(TreeItem *item);  
  337.   void unhiliteDropItem(TreeItem *item);
  338.   void invalidateMetrics();
  339.  
  340. private:
  341.   TreeItemList items;    // root-level stuff
  342.  
  343.   TreeItem *curSelected;
  344.  
  345.     BltCanvas *dCanvas;
  346.  
  347.   void drawItems(Canvas *c);
  348.   void setCurItem(TreeItem *item, BOOL expandCollapse=TRUE, BOOL editifselected=FALSE);
  349.   void countSubItems(PtrList<TreeItem> &drawlist, TreeItemList *list, int indent, int *c, int *m, int z);
  350.   void getMetrics(int *numItemsShow, int *maxWidth);
  351.   void ensureMetricsValid();
  352.   int getLinkLine(TreeItem *item, int level);
  353.   void endEditLabel(char *newlabel);
  354.   void editUpdate();
  355.   int jumpToNextSubItems(TreeItemList *list, char c);
  356.  
  357.   int itemHeight;
  358.  
  359.   SkinBitmap *tabClosed, *tabOpen;
  360.   SkinBitmap *linkTopRight, *linkTopBottom, *linkTopRightBottom;
  361.   SkinBitmap *linkTabTopRight, *linkTabTopBottom, *linkTabTopRightBottom;
  362.   
  363.   TreeItem *firstItemVisible;
  364.   TreeItem *lastItemVisible;
  365.  
  366.   TreeItem *mousedown_item, *prevbdownitem;
  367.   POINT mousedown_anchor;
  368.   BOOL mousedown_dragdone;
  369.   TreeItem *hitItem,        // the dest item
  370.            *draggedItem;    // the source item
  371.  
  372.   int inHitTest;
  373.  
  374.   BOOL metrics_ok;
  375.   int maxWidth;
  376.   int maxHeight;
  377.  
  378.   String defaultTip;
  379.  
  380.   const char *getLiveTip();
  381.   void setLiveTip(const char *tip);
  382.   TreeItem *tipitem;
  383.  
  384.   BOOL redraw;
  385.  
  386.   PtrList<TreeItem> drawList;
  387.   TreeItem *edited;
  388.  
  389.   EditWnd *editwnd;
  390.   char editbuffer[256];
  391.  
  392.   int deleteItems;
  393.   BOOL firstFound;
  394.  
  395.   TreeItem *currentItem;
  396.   String hilitedColorName;
  397.   SkinColor hilitedColor;
  398.   int autoedit;
  399.   int autocollapse;
  400.   int textsize;
  401. };
  402.  
  403. template<class T> class TreeItemParam : public TreeItem {
  404. public:
  405.   TreeItemParam(T _param) { param = _param; }
  406.  
  407.   T getParam() { return param; }
  408.   operator T() { return getParam(); }
  409.  
  410. private:
  411.   T param;
  412. };
  413.  
  414. #endif
  415.